package cevent.source.cloudcenter.server.listener;/**
 * Created by Cevent on 2021/7/15.
 */

import cevent.source.cloudcenter.server.domain.Store;
import cevent.source.cloudcenter.server.dto.StoreExcelDto;
import cevent.source.cloudcenter.server.mapper.StoreMapper;
import cevent.source.cloudcenter.server.util.UUID8Util;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author cevent
 * @description 读取店铺excel监听器
 * @date 2021/7/15 18:18
 */
public class StoreExcelDtoListener extends AnalysisEventListener<StoreExcelDto> {
    private static final Logger LOG= LoggerFactory.getLogger(StoreExcelDtoListener.class);

    //通过新建属性获取mapper
    private StoreMapper storeMapper;
    //有参构造生成mapper
    public StoreExcelDtoListener(StoreMapper storeMapper) {
        this.storeMapper = storeMapper;
    }

    //获取每行数据读取，从第二行开始
    @Override
    public void invoke(StoreExcelDto storeExcelDto, AnalysisContext analysisContext) {
        //1.获取数据
        LOG.info("监听获取的store表数据：{}",storeExcelDto);
        String id=String.valueOf(storeExcelDto.getOrderId());
        id= UUID8Util.getShortUUID();
        storeExcelDto.setProvince("");
        storeExcelDto.setCity("");
        storeExcelDto.setDistrict("");
        storeExcelDto.setTypeId("");
        storeExcelDto.setPayStatus("");
        storeExcelDto.setStoreStatus("");
        storeExcelDto.setStoreFrontType("");
        storeExcelDto.setSubsidy(new BigDecimal("600.00"));
        storeExcelDto.setSubsidyType("UN");

        storeExcelDto.setCreateTime("");
        storeExcelDto.setUpdateTime("");
        Store store=new Store();
        BeanUtils.copyProperties(storeExcelDto,store);
        store.setUniId(id);
        //日期转字符串，再用simpleDateFormat转pase to date（需要try catch捕获）
        Date now=new Date();
        String simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(now);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
        Date date= null;
        try {
            date = dateFormat.parse(simpleDateFormat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        store.setCreateTime(date);
        store.setUpdateTime(date);
        storeMapper.insertSelective(store);
    }
    //读取第一行内容
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        super.invokeHeadMap(headMap, context);
    }
    //读取完毕执行
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
